﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace BenevolentSunData
{
    public class TorchCell : Cell
    {

        public override string Name
        {
            get
            {
                if (IsOn)
                    return "Lit Torch";
                return "Unlit Torch";
            }
        }

        public override string Note
        {
            get
            {
                if (!IsOn)
                    return "Hit with the beam to illuminate.";
                return "";
            }
        }

        #region State
        protected bool isOn = true;
        [ContentSerializerIgnore]
        public bool IsOn
        {
            get { return isOn; }
            set
            {
                if (isOn == value)
                    return;
                if (isOn)
                    Map.PointLights.Remove(pointLight);
                isOn = value;
                if (isOn)
                    Map.PointLights.Add(pointLight);
                //pointLight.UpdateLight();
                pointLight.Color = new Color(lightColor);
            }
        }

        protected Vector3 lightColor = Color.Yellow.ToVector3();
        [ContentSerializerAttribute(Optional = true)]
        public Vector3 LightColor
        {
            get { return lightColor; }
            set {
                //if (lightColor == value)
                //    return;
                lightColor = value;
                pointLight.Color = new Color(lightColor);
            }
        }

        public static float ComputeFallOff(float dist) 
        {
            return Math.Max(1f - ((dist - 39f) / 400f), 0f);
        }
        #endregion


        #region Lighting
        PointLight pointLight = new PointLight();
        #endregion


        #region Graphics
        [ContentSerializerIgnore]
        public Model Model;

        [ContentSerializerIgnore]
        public Material Material = new Material();

        Matrix[] boneTransforms;
        #endregion


        #region Drawing
        public override void Draw(Vector3 position)
        {
            foreach (ModelMesh mesh in Model.Meshes)
            {
                Matrix world =
                    boneTransforms[mesh.ParentBone.Index] *
                    Matrix.CreateFromYawPitchRoll(Rotation, 0f, 0f) *
                    Matrix.CreateTranslation(position);
                Material.DrawMesh(mesh, world, Map.PointLights, Map.BeamLights);
            }
        }
        #endregion


        #region Initialization
        public TorchCell()
        {
        }

        public override void LoadContent(ContentManager Content, Map map)
        {
            Material = Material = map.Skin.GetMaterial("Torch");
            Model = Content.Load<Model>("Models/Torch");
            boneTransforms = new Matrix[Model.Bones.Count];
            Model.CopyAbsoluteBoneTransformsTo(boneTransforms);
            pointLight.Initialize(map.SwitchEffect);
            base.LoadContent(Content, map);
        }
        #endregion


        #region Beams
        public override void Clear()
        {
            base.Clear();
        }

        public override List<Direction> AddInput(Beam beam, Direction direction)
        {
            MergeBeam(beam, direction);

            IsOn = true;

            List<Direction> output = new List<Direction>();

            if (MergeBeam(beam, DirectionUtils.Invert(direction)))
                output.Add(DirectionUtils.Invert(direction));

            return output;
        }
        #endregion


        #region ICloneable
        public override object Clone()
        {
            TorchCell cell = new TorchCell();
            cell.Coords = new Point(Coords.X, Coords.Y);
            cell.Model = Model;
            cell.Material = cell.Material = Material.Clone();
            cell.boneTransforms = boneTransforms;
            cell.lightColor = new Vector3(lightColor.X, lightColor.Y, lightColor.Z);
            cell.floorBoneTransforms = floorBoneTransforms;
            cell.floorModel = floorModel;
            cell.floorMaterial = floorMaterial.Clone();
            cell.pointLight.Initialize(Map.SwitchEffect);

            cell.pointLight.Position = new Vector3(cell.Coords.X, 4.5f, cell.Coords.Y) * 39f;
            cell.pointLight.Range = 600;
            cell.pointLight.Color = new Color(lightColor);

            return cell;
        }
        #endregion

    }
}
